/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.web.controller;

import cc.altius.model.CallType;
import cc.altius.model.Disposition;
import cc.altius.model.Doctor;
import cc.altius.model.SmsDetails;
import cc.altius.service.CustomerService;
import cc.altius.service.DoctorService;
import cc.altius.service.SmsService;
import cc.altius.service.UserService;
import cc.altius.utils.DateUtils;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author sagar
 */
@Controller
public class ReportController {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private DoctorService doctorService;
    @Autowired
    private UserService userService;
    @Autowired
    private SmsService smsService;

    @ModelAttribute("callTypeList")
    List<CallType> getCallTypeList() {
        return this.customerService.getCallTypeList();
    }

    @ModelAttribute("dispositionList")
    List<Disposition> getDispositionList() {
        List<Disposition> list = this.customerService.getDispositionList();
        System.out.println("list" + list.get(0).getDispositionId());
        return list;
    }

    @ModelAttribute("doctorList")
    public List<Doctor> populateDoctorList() {
        return this.doctorService.getDoctorList();
    }

    @ModelAttribute("userList")
    List<cc.altius.domain.User> populateUserList() {
        List<cc.altius.domain.User> userList = this.userService.getUserListForReport();
        return userList;
    }

    @RequestMapping("/reports/callLogReports.htm")
    public String showReport(@RequestParam(value = "startDate", required = true, defaultValue = "0") String startDate,
            @RequestParam(value = "stopDate", required = true, defaultValue = "0") String stopDate,
            @RequestParam(value = "callTypeId", required = true, defaultValue = "0") int callTypeId,
            @RequestParam(value = "dispositionId", required = true, defaultValue = "0") int dispositionId,
            @RequestParam(value = "doctorId", required = true, defaultValue = "0") int doctorId,
            @RequestParam(value = "userId", required = true, defaultValue = "0") int userId,
            ModelMap modelMap) {

        System.out.println("inside reports");
        if (startDate.equals("0")) {
            startDate = DateUtils.getCurrentDateString(DateUtils.IST, DateUtils.YMD);
        }

        if (stopDate.equals("0")) {
            stopDate = DateUtils.getCurrentDateString(DateUtils.IST, DateUtils.YMD);
        }
        List<Map<String, Object>> data = this.customerService.getCallLogReport(startDate, stopDate, callTypeId, dispositionId, doctorId, userId);
        modelMap.addAttribute("data", data);
        modelMap.addAttribute("startDate", startDate);
        modelMap.addAttribute("stopDate", stopDate);
        modelMap.addAttribute("callTypeId", callTypeId);
        modelMap.addAttribute("dispositionId", dispositionId);
        modelMap.addAttribute("doctorId", doctorId);
        modelMap.addAttribute("userId", userId);
        for (Map m : data) {
//            System.out.println(m.keySet());
//            System.out.println(m.get("DISPOSITION_NAME"));
            modelMap.addAttribute("dispositionName", m.get("DISPOSITION_NAME"));
        }

        return "reports/callLogReports";
    }

    @RequestMapping("/reports/smsReport.htm")
    public String showSmsGetReport(@RequestParam(value = "startDate", required = true, defaultValue = "0") String startDate,
            @RequestParam(value = "stopDate", required = true, defaultValue = "0") String stopDate,
            ModelMap modelMap) {
        if (startDate.equals("0")) {
            startDate = DateUtils.getCurrentDateString(DateUtils.IST, DateUtils.YMD);
        }

        if (stopDate.equals("0")) {
            stopDate = DateUtils.getCurrentDateString(DateUtils.IST, DateUtils.YMD);
        }
        List<Map<String, Object>> lstSms = this.smsService.reportSmsStatus(startDate + " 00:00:00", stopDate + " 23:59:59");
        modelMap.addAttribute("startDate", startDate);
        modelMap.addAttribute("stopDate", stopDate);
        modelMap.addAttribute("lstSms", lstSms);
        return "reports/smsReport";
    }

//    @RequestMapping(value = "reports/faliledSmsForReport.htm", method = RequestMethod.GET)
//    public String getFaliledSmsDateWise(@RequestParam(value = "startDate", required = true, defaultValue = "0") String startDate,
//            @RequestParam(value = "stopDate", required = true, defaultValue = "0") String stopDate,
//            ModelMap modelMap) {
//        System.out.println("startDate:" +startDate);
//        System.out.println("stopDate:" +stopDate);
//        if (startDate.equals("0")) {
//            startDate = DateUtils.getCurrentDateString(DateUtils.IST, DateUtils.YMD);
//        }
//        
//        if (stopDate.equals("0")) {
//            stopDate = DateUtils.getCurrentDateString(DateUtils.IST, DateUtils.YMD);
//        }
//        List<SmsDetails> failedSmsList = this.smsService.getFailedSmsListDateWise(startDate, stopDate);
//        modelMap.addAttribute("failedSmsList",failedSmsList);
//        return "reports/faliledSms";
//    }
    @RequestMapping(value = "reports/failedSmsStatus.htm", method = RequestMethod.GET)
    public String getFaliledSmsStatus(@RequestParam(value = "startDate", required = true, defaultValue = "0") String startDate,
            @RequestParam(value = "stopDate", required = true, defaultValue = "0") String stopDate,
            ModelMap modelMap) {
        System.out.println("in getFaliledSmsStatus");
        System.out.println("startDate :" + startDate);
        System.out.println("stopDate :" + stopDate);
        List<SmsDetails> failedSmsDailyStatus = this.smsService.getFailedSmsListForReport(startDate, stopDate);
        System.out.println("failedSmsDailyStatus: " + failedSmsDailyStatus);
        modelMap.addAttribute("failedSmsDailyStatus", failedSmsDailyStatus);
        if ("0".equals(startDate) && "0".equals(stopDate)) {
            //for home page
            return "reports/failedSmsDailyStatus";
        } else {
            //for report page
            return "reports/failedSmsStatus";
        }
    }

    @RequestMapping(value = "reports/successSmsStatus.htm", method = RequestMethod.GET)
    public String getSuccessSmsStatus(@RequestParam(value = "startDate", required = true, defaultValue = "0") String startDate,
            @RequestParam(value = "stopDate", required = true, defaultValue = "0") String stopDate,
            ModelMap modelMap) {
        System.out.println("in getSuccessSmsStatus");
        System.out.println("startDate :" + startDate);
        System.out.println("stopDate :" + stopDate);
        List<SmsDetails> successSmsDailyStatus = this.smsService.getSuccessSmsListForReport(startDate, stopDate);
        modelMap.addAttribute("successSmsDailyStatus", successSmsDailyStatus);
        return "reports/successSmsStatus";
    }

    @RequestMapping(value = "reports/pendingSmsStatus.htm", method = RequestMethod.GET)
    public String getPendingSmsStatus(@RequestParam(value = "startDate", required = true, defaultValue = "0") String startDate,
            @RequestParam(value = "stopDate", required = true, defaultValue = "0") String stopDate,
            ModelMap modelMap) {
        System.out.println("in getPendingSmsStatus");
        System.out.println("startDate :" + startDate);
        System.out.println("stopDate :" + stopDate);
        List<SmsDetails> pendingSmsDailyStatus = this.smsService.getPendingSmsListForReport(startDate, stopDate);
        modelMap.addAttribute("pendingSmsDailyStatus", pendingSmsDailyStatus);
        return "reports/pendingSmsStatus";
    }

    @RequestMapping(value = "/reports/resendSms.htm", method = RequestMethod.GET)
    public String resendSms(@RequestParam("smsId") int smsId, ModelMap map) {
        try {
            this.smsService.resendSmsViaButton(smsId);
            return "redirect:failedSmsStatus.htm";
        } catch (Exception e) {
            e.printStackTrace();

            return "redirect:failedSmsStatus.htm";
        }
    }
}

    
